//
//  RCActivityDiscountListDataModel.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-27.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCActivityDiscountListDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NSArray+EX.h"

@implementation RCActivityDiscountListItem

@synthesize activityID = _activityID;
@synthesize activityName = _activityName;
@synthesize activityPicUrl = _activityPicUrl;
@synthesize activityTime = _activityTime;
@synthesize activityHots = _activityHots;
@synthesize activitySignup = _activitySignup;
@synthesize activityOriginalPrice = _activityOriginalPrice;
@synthesize activityPresentPrice = _activityPresentPrice;
@synthesize activityDistance = _activityDistance;

-(id)init{
    self = [super init];
    if (self) {
        _activityID = nil;
        _activityName = nil;
        _activityPicUrl = nil;
        _activityTime = nil;
        _activityHots = 0;
        _activitySignup = 0;
        _activityOriginalPrice = 0;
        _activityPresentPrice = 0;
        _activityDistance = nil;
    }
    return self;
}

-(void)dealloc{
    self.activityID = nil;
    self.activityName = nil;
    self.activityPicUrl = nil;
    self.activityDistance = nil;
    self.activityTime = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCActivityDiscountListItem *retObject = nil;
    retObject = [[RCActivityDiscountListItem alloc]init];
    if (nil == retObject) {
        return nil;
    }
    retObject.activityID = [data stringValueForKey:@"id" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.activityName = [data stringValueForKey:@"title" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.activityPicUrl = [data stringValueForKey:@"picurl" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.activityTime = [data stringValueForKey:@"activitytime" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.activityHots = [data intValueForKey:@"hots" defaultValue:0];
    retObject.activitySignup = [data intValueForKey:@"signup" defaultValue:0];
    retObject.activityOriginalPrice = [data intValueForKey:@"originalprice" defaultValue:0];
    retObject.activityPresentPrice = [data intValueForKey:@"presentprice" defaultValue:0];
    CGFloat distance = [data floatValueForKey:@"distance" defaultValue:0.0];
    NSString *distanceStr = nil;
    if (distance > 1000) {
        if (distance/1000 > 999) {
            distanceStr = @"999+km";
        }else{
            distanceStr = [NSString stringWithFormat:@"%.2fkm",distance/1000];
        }
        
    }else{
        distanceStr = [NSString stringWithFormat:@"%dm",(NSInteger)distance];
    }
    retObject.activityDistance = distanceStr;
    return [retObject autorelease];
}

@end

@implementation RCActivityDiscountListDataModel

@synthesize total = _total;
@synthesize pn = _pn;
@synthesize rn = _rn;
@synthesize itemListArray = _itemListArray;

-(id)init{
    self = [super init];
    if (self) {
        _itemListArray = nil;
        _total = 0;
    }
    return self;
}

-(void)dealloc{
    self.itemListArray = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    NSDictionary *dict = [data dictionaryValueForKey:@"data" defaultValue:nil];
    if (nil == dict) {
        return nil;
    }
    
    NSArray *dictArray = [dict arrayValueForKey:@"items" defaultValue:nil];
    if (nil == dictArray || [dictArray count] == 0) {
        return nil;
    }
    
    RCActivityDiscountListDataModel *retObject = nil;
    retObject = [[RCActivityDiscountListDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    
    retObject.total = [dict intValueForKey:@"total" defaultValue:0];
    retObject.pn = [dict intValueForKey:@"pn" defaultValue:0];
    retObject.rn = [dict intValueForKey:@"rn" defaultValue:0];
    
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithCapacity:[dictArray count]];
    for (int i = 0; i < [dictArray count]; i++) {
        NSDictionary *itemDict = [dictArray safeObjectAtIndex:i];
        if (nil == itemDict) {
            continue;
        }
        RCActivityDiscountListItem *item = [RCActivityDiscountListItem parseDataFromJSON:itemDict];
        if (nil == item) {
            continue;
        }
        [tempArray addObject:item];
    }
    retObject.itemListArray = [NSMutableArray arrayWithArray:tempArray];
    [tempArray release];
    return [retObject autorelease];
}

@end
